/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.finance;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.finance.Bvaccnt;
import src.domain.gl.Glaccnt;
import src.services.finance.BvaccntModel;
import src.services.gl.GlaccntConverter;
import src.services.gl.GlaccntModel;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class BvaccntBean implements Serializable {

    private List<Bvaccnt> bvaccnts;
    private List<Glaccnt> avaibleGL;
    private Bvaccnt selectedData, insertedData;
    private boolean editMode = true;
    private BvaccntModel helper;

    public BvaccntBean() {
        this.helper = new BvaccntModel();
        this.bvaccnts = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
        this.avaibleGL = GlaccntConverter.glaccountDB;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public Bvaccnt getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Bvaccnt selectedData) {
        this.selectedData = selectedData;
    }
    
    public Bvaccnt getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Bvaccnt insertedData) {
        this.insertedData = insertedData;
    }

    public List<Glaccnt> getAvaibleGL() {
        return avaibleGL;
    }

    public List<Bvaccnt> getBvaccnts() {
        return bvaccnts;
    }
    
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void initForm() {
        
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        insertedData = Bvaccnt.clone(selectedData);
    }
    
    public void newData() {  
        initForm();
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Bvaccnt();
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
        
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.bvaccnts.add(insertedData);
            else {
                int i = bvaccnts.indexOf(selectedData);
                if (i <= bvaccnts.size()) bvaccnts.set(i, Bvaccnt.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setBvaccntId(null);
        } 
        
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.bvaccnts.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }
    
}
